Skip to content

Allow /api/exit to stop all producers before terminating#2115

Open
btgoodwin wants to merge 7 commits intoAlexxIT:masterfrom
btgoodwin:stop-producers-on-api-exit-handler
Open

Allow /api/exit to stop all producers before terminating#2115
btgoodwin wants to merge 7 commits intoAlexxIT:masterfrom
btgoodwin:stop-producers-on-api-exit-handler

Conversation

@btgoodwin
Copy link
Copy Markdown

@btgoodwin btgoodwin commented Feb 25, 2026

This patch refactors some code to make it possible for the api package to refer to the streams package so that the /api/exit endpoint can call a new streams API: StopAll. The method stops all producers of any kind. In the case of rtsp sources, this results in a TEARDOWN message being sent to the device, allowing it to free any onboard resources that were dedicated to go2rtc having configured that source/stream.

Fixes #2104

Testing (RTSP):

  1. Start go2rtc.
  2. Configure an rtsp source
  3. Access the configured source's manufacturer UI/configuration app to observe the access log. I'm using an Axis camera for this.
  4. Access the go2rtc 1984 application page and create a tab from the configured source's stream link.
  5. Observe on (2) that a stream is now active; my Axis shows this:
    2021-03-28T14:14:32.700-05:00 axis-accc8eac4517 [ NOTICE  ] monolith: RTSP UNKNOWN session U3xyCnUGHI_kLa0j created from 192.168.1.224
    
  6. POST to the /api/exit endpoint.
  7. Observe that the application has exited.
  8. Observe in (2) that the stream is no longer active. For Axis, it shows terminated; if you observe this on Wireshark, you will see a TEARDOWN event over port 554.
    2021-03-28T14:21:40.004-05:00 axis-accc8eac4517 [ NOTICE  ] monolith: RTSP video/x-h264 session rSQoJw8.xdc-cnh3 terminated from 192.168.1.224
    

In all cases where this was used by other packages, those packages also
imported 'streams' and in some cases this eliminated the dependency on
'api' for that package.
@btgoodwin btgoodwin changed the title [#2104] Allow /api/exit to stop all producers before terminating Allow /api/exit to stop all producers before terminating Feb 25, 2026
@btgoodwin btgoodwin force-pushed the stop-producers-on-api-exit-handler branch from de88d89 to 72ef3b9 Compare February 25, 2026 18:30
By moving these to a separate package, the other packages can refer to
it, which leaves the original api package only as handlers for the
top-level APIs.  This also allows for those handlers to refer to the
other packages.
Optional 'force' argument, when true, results in stopping all producers
regardless of tracks, consumers, etc.  This provides the application a
means to "clean up" active streams.
This method iterates over all the stream instances and calls
StopProducers(true) to force stopping all producers in every stream as a
means to "clean up" those connections, if necessary.
By allowing all producers to stop, modules like rtsp will send
'TEARDOWN' messages to the remote device, allowing that remote device to
free up any resources utilized by go2rtc to provide the stream during
configuration.
@btgoodwin btgoodwin force-pushed the stop-producers-on-api-exit-handler branch from 72ef3b9 to a1b5e75 Compare February 25, 2026 18:33
This allows us to pass strings/characters to stdin for exec processes
that can take a more graceful end.
This lets ffmpeg quit via passing 'q' to stdin, which is what it expects
when run as a shell process.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ffmpeg -based stream does not send TEARDOWN to camera

1 participant